//座位管理
#include "tab2.h"

// 座位管理相关变量
static lv_obj_t *train_table = NULL;
static lv_obj_t *seat_modal = NULL;
static lv_obj_t *seat_buttons[256] = {NULL}; // 假设最多100个座位
static Train *current_train = NULL;
static int current_seat_type = 0;



// 刷新班次表格
static void refresh_train_table() 
{
    if (!train_table) return;
    
    // 清除现有内容
    lv_table_set_row_cnt(train_table, 1); // 保留标题行
    
    // 确保trainList有效
    if (trainList) 
    {
        freeTrainList(trainList);
    }
    
    trainList = initTrainList();
    if (!trainList) 
    {
        printf("错误: 初始化班次链表失败\n");
        return;
    }
    
    if (!loadTrainsFromFile(trainList, "bendi.json")) 
    {
        printf("错误: 加载班次数据失败\n");
        return;
    }
    
    if (!train_table || !trainList) return;
    
    // 设置表格标题
    lv_table_set_cell_value(train_table, 0, 0, "车次");
    lv_table_set_cell_value(train_table, 0, 1, "起始站");
    lv_table_set_cell_value(train_table, 0, 2, "终点站");
    lv_table_set_cell_value(train_table, 0, 3, "出发时间");
     lv_table_set_cell_value(train_table, 0, 4, "到达时间");
    lv_table_set_cell_value(train_table, 0, 5, "日期");
    lv_table_set_cell_value(train_table, 0, 6, "操作");
    
    // 添加班次数据
    Train *current = trainList->head;
    int row = 1;
    while (current) 
    {
        lv_table_set_row_cnt(train_table, row + 1);
        
        // 车次信息
        lv_table_set_cell_value(train_table, row, 0, current->TrainNumber);
        lv_table_set_cell_value(train_table, row, 1, current->start);
        lv_table_set_cell_value(train_table, row, 2, current->end);
        lv_table_set_cell_value(train_table, row, 3, current->DepartTime);
        lv_table_set_cell_value(train_table, row, 4, current->ArriveTime);
        lv_table_set_cell_value(train_table, row, 5, current->date);
        
        // 操作按钮
        lv_table_set_cell_value(train_table, row, 6, "管理座位");
        
        current = current->next;
        row++;
    }
}

// 关闭座位模态窗口的回调函数
static void close_seat_modal_cb(lv_event_t *e) {
    if (seat_modal) {
        lv_obj_del(seat_modal);
        seat_modal = NULL;
        current_train = NULL;
        current_seat_type = 0;
        memset(seat_buttons, 0, sizeof(seat_buttons));
    }
}

// 座位按钮点击事件
static void seat_button_cb(lv_event_t *e) 
{
    lv_obj_t *target = lv_event_get_target(e);
    int seat_index = (int)lv_event_get_user_data(e);
    
    // 检查当前班次和座位类型是否有效
    if (!current_train || current_seat_type < 0 || current_seat_type >= current_train->seatCount) 
    {
        printf("错误: 班次或座位类型无效\n");
        return;
    }
    
    SeatInfo *seat_info = &current_train->seats[current_seat_type];
    if (seat_index < 0 || seat_index >= seat_info->TotalSeats) 
    {
        printf("错误: 座位索引越界\n");
        return;
    }
    
    // 切换座位状态
    if (toggleSeatStatus(current_train, current_seat_type, seat_index)) 
    {
        // 更新按钮样式
        if (seat_info->SeatStatus[seat_index] == '1') {
            lv_obj_set_style_bg_color(target, lv_color_hex(0x00FF00), 0); // 绿色-可用
        } else {
            lv_obj_set_style_bg_color(target, lv_color_hex(0xFF0000), 0); // 红色-不可用
        }
        
        // 保存到文件
        saveTrainsToFile(trainList, "bendi.json");
    }
}

// 显示座位管理界面
static void show_seat_management(Train *train, int seat_type) 
{

    
    // 加强验证
    if (!train || !trainList) 
    {
        printf("错误: 班次或链表数据无效\n");
        return;
    }
    
    // 重新验证train是否仍在链表中
    Train *valid_train = findTrain(trainList, train->TrainNumber);
    if (!valid_train || valid_train != train) 
    {
        printf("错误: 班次数据已过期\n");
        return;
    }
    
    if (!train->seats) {
        printf("错误: 班次座位数据无效\n");
        return;
    }
    
    if (seat_type < 0 || seat_type >= train->seatCount) 
    {
        printf("错误: 座位类型索引越界\n");
        return;
    }
    
    if (!train->seats[seat_type].SeatStatus || !train->seats[seat_type].SeatNumbers) 
    {
        printf("错误: 座位状态或号码数据无效\n");
        return;
    }
    
    current_train = train;
    current_seat_type = seat_type;
    
    // 创建模态窗口
    if (seat_modal) 
    {
        lv_obj_del(seat_modal);
        seat_modal = NULL;
    }
    
    seat_modal = lv_obj_create(lv_scr_act());
    lv_obj_set_size(seat_modal, 700, 500);
    lv_obj_align(seat_modal, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_bg_opa(seat_modal, LV_OPA_COVER, 0);
    lv_obj_set_style_bg_color(seat_modal, lv_color_hex(0x2D2D3A), 0);
    lv_obj_set_style_border_width(seat_modal, 2, 0);
    lv_obj_set_style_border_color(seat_modal, lv_color_white(), 0);
    
    // 标题
    lv_obj_t *title = lv_label_create(seat_modal);
    char title_text[100];
    snprintf(title_text, sizeof(title_text), "座位管理: %s - %.2f元", 
             train->TrainNumber, train->seats[seat_type].SeatPrice);
    lv_label_set_text(title, title_text);
    lv_obj_set_style_text_font(title, font_chinese, 0);
    lv_obj_set_style_text_color(title, lv_color_white(), 0);
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 10);
    
    // 可用座位信息
    lv_obj_t *info_label = lv_label_create(seat_modal);
    char info_text[256];
    snprintf(info_text, sizeof(info_text), "可用座位: %d/%d", 
             train->seats[seat_type].AvailableSeats, train->seats[seat_type].TotalSeats);
    lv_label_set_text(info_label, info_text);
    lv_obj_set_style_text_font(info_label, font_chinese, 0);
    lv_obj_set_style_text_color(info_label, lv_color_white(), 0);
    lv_obj_align(info_label, LV_ALIGN_TOP_LEFT, 20, 50);
    
    // 创建座位网格
    int cols = 10; // 每行10个座位
    int rows = (train->seats[seat_type].TotalSeats + cols - 1) / cols;
    
    lv_obj_t *container = lv_obj_create(seat_modal);
    lv_obj_set_size(container, 650, 350);
    lv_obj_align(container, LV_ALIGN_TOP_LEFT, 20, 80);
    lv_obj_set_style_bg_opa(container, LV_OPA_TRANSP, 0);
    lv_obj_set_style_border_width(container, 0, 0);
    
    // 创建座位按钮
    for (int i = 0; i < train->seats[seat_type].TotalSeats; i++) 
    {
        int row = i / cols;
        int col = i % cols;
        
        lv_obj_t *btn = lv_btn_create(container);
        lv_obj_set_size(btn, 50, 30);
        lv_obj_set_pos(btn, col * 60 + 10, row * 40 + 10);
        
        // 根据座位状态设置颜色
        if (train->seats[seat_type].SeatStatus[i] == '1') 
        {
            lv_obj_set_style_bg_color(btn, lv_color_hex(0x00FF00), 0); // 绿色-可用
        } 
        else 
        {
            lv_obj_set_style_bg_color(btn, lv_color_hex(0xFF0000), 0); // 红色-不可用
        }
        
        // 设置座位号
        lv_obj_t *label = lv_label_create(btn);
        lv_label_set_text(label, train->seats[seat_type].SeatNumbers[i]);
        lv_obj_set_style_text_font(label, font_chinese, 0);
        lv_obj_center(label);
        
        // 添加点击事件
        lv_obj_add_event_cb(btn, seat_button_cb, LV_EVENT_CLICKED, (void*)(intptr_t)i);
        
        seat_buttons[i] = btn;
    }
    
    // 关闭按钮
    lv_obj_t *close_btn = lv_btn_create(seat_modal);
    lv_obj_set_size(close_btn, 100, 40);
    lv_obj_align(close_btn, LV_ALIGN_BOTTOM_RIGHT, -20, -20);
    lv_obj_t *close_label = lv_label_create(close_btn);
    lv_label_set_text(close_label, "关闭");
    lv_obj_set_style_text_font(close_label, font_chinese, 0);
    lv_obj_center(close_label);
    lv_obj_add_event_cb(close_btn, close_seat_modal_cb, LV_EVENT_CLICKED, NULL);
}

// 班次表格点击事件处理
static void train_table_event_cb(lv_event_t *e) 
{
    lv_obj_t *target = lv_event_get_target(e);
    lv_event_code_t code = lv_event_get_code(e);
    
    if (code == LV_EVENT_VALUE_CHANGED) 
    {
        uint16_t col, row;
        lv_table_get_selected_cell(target, &row, &col);
        
        if (row == 0) return; // 标题行
        
        // 获取车次号
        const char *train_number = lv_table_get_cell_value(target, row, 0);
        
        // 根据车次号查找班次
        Train *train = findTrain(trainList, train_number);
        
        if (!train) {
            printf("错误: 未找到对应班次 %s\n", train_number);
            return;
        }
        
        if (col == 6) 
        { // 点击操作列，显示座位管理
            // 检查班次是否有座位类型
            if (train->seatCount > 0) {
                show_seat_management(train, 0);
            } else {
                // 提示没有座位信息
                printf("该班次没有座位信息\n");
            }
        }
    }
}

// 刷新按钮点击事件
static void refresh_train_btn_event_cb(lv_event_t *e) 
{
    refresh_train_table();
}

// 初始化座位管理界面
void init_user_management_tab2(lv_obj_t *parent) 
{
    // 创建容器
    lv_obj_t *container = lv_obj_create(parent);
    lv_obj_set_size(container, lv_pct(100), lv_pct(100));
    lv_obj_set_style_border_width(container, 0, 0);
    lv_obj_set_style_bg_opa(container, LV_OPA_TRANSP, 0);
    
    // 标题
    lv_obj_t *title = lv_label_create(container);
    lv_label_set_text(title, "座位管理");
    lv_obj_set_style_text_font(title, font_chinese, 0);
    lv_obj_set_style_text_color(title, lv_color_white(), 0);
    lv_obj_align(title, LV_ALIGN_TOP_LEFT, 20, 10);
    
    
    // 刷新按钮
    lv_obj_t *refresh_btn = lv_btn_create(container);
    lv_obj_set_size(refresh_btn, 100, 40);
    lv_obj_align(refresh_btn, LV_ALIGN_TOP_RIGHT, -20, 10);
    lv_obj_t *refresh_label = lv_label_create(refresh_btn);
    lv_label_set_text(refresh_label, "刷新");
    lv_obj_set_style_text_font(refresh_label, font_chinese, 0);
    lv_obj_center(refresh_label);
    lv_obj_add_event_cb(refresh_btn, refresh_train_btn_event_cb, LV_EVENT_CLICKED, NULL);
    
    // 创建班次表格
    train_table = lv_table_create(container);
    lv_obj_set_size(train_table, lv_pct(95), lv_pct(80));
    lv_obj_align(train_table, LV_ALIGN_TOP_MID, 0, 60);
    lv_obj_set_style_text_font(train_table, font_chinese, 0);
    
    // 设置表格列宽
    lv_table_set_col_width(train_table, 0, 100);
    lv_table_set_col_width(train_table, 1, 100);
    lv_table_set_col_width(train_table, 2, 100);
    lv_table_set_col_width(train_table, 3, 100);
    lv_table_set_col_width(train_table, 4, 100);
    lv_table_set_col_width(train_table, 5, 100);
    lv_table_set_col_width(train_table, 6, 100);
    
    // 设置表格标题
    lv_table_set_cell_value(train_table, 0, 0, "车次");
    lv_table_set_cell_value(train_table, 0, 1, "起始站");
    lv_table_set_cell_value(train_table, 0, 2, "终点站");
    lv_table_set_cell_value(train_table, 0, 3, "出发时间");
    lv_table_set_cell_value(train_table, 0, 4, "到达时间");
    lv_table_set_cell_value(train_table, 0, 5, "日期");
    lv_table_set_cell_value(train_table, 0, 6, "操作");
    
    // 设置表格事件
    lv_obj_add_event_cb(train_table, train_table_event_cb, LV_EVENT_ALL, NULL);
    
    // 初始加载数据
    refresh_train_table();
}